package com.bwie.controller;
import com.bwie.pojo.Order;
import com.bwie.pojo.Student;
import com.bwie.service.Impl.OrderServiceImpl;
import com.bwie.service.OrderService;
import com.bwie.utils.R;
import com.bwie.vo.PageInfoVo;
import org.elasticsearch.index.query.IdsQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.IndexOperations;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.document.Document;
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.web.bind.annotation.*;

import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
* (t_order)表控制层
*
* @author xxxxx
*/
@RestController
@RequestMapping("/order/order")
public class OrderController {
/**
* 服务对象
*/
    @Autowired
    private OrderService orderService;
    @Autowired
    private ElasticsearchRestTemplate restTemplate;
    @PostMapping("/orderList")
    public R orderList(
            @RequestBody PageInfoVo pageInfoVo
            ){
        return orderService.orderList(pageInfoVo);
    }
    @PostMapping("/updateOrder")
    public R updateOrder(
        @RequestBody Order order
    ){
        return orderService.updateOrder(order);
    }
    @PostMapping("/deleteOrder/{orderId}")
    public R deleteOrder(
        @PathVariable(name = "orderId")Integer orderId
    ){
        return orderService.deleteOrder(orderId);
    }
    @PostMapping("/deleteAll")
    public R deleteAll(
            @RequestBody List<Order> orderList
            ){
        return orderService.deleteAll(orderList);
    }

    /**
     * 添加索引
     */
    @PostMapping("/addStuIndex")
    public R addStuIndex(
            @RequestBody Student student
            ){
        IndexOperations indexOperations = restTemplate.indexOps(Student.class);
        if(indexOperations.exists()){
            indexOperations.create();
            Document mapping = indexOperations.createMapping();
            indexOperations.putMapping(mapping);
        }
        restTemplate.save(student);
        return R.OK();
    }

    /**
     * 批量添加
     * @param studentList
     * @return
     */
    @PostMapping("/addStudentAll")
    public R addStudentAll(
        @RequestBody List<Student> studentList
    ){
        for (Student student : studentList) {
            restTemplate.save(student);
        }
        return R.OK();
    }

    /**
     * 全量修改
     * @param student
     * @return
     */
    @PostMapping("/updateStudent")
    public R updateStudent(
        @RequestBody Student student
    ){
        restTemplate.save(student);
        return R.OK();
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @PostMapping("/delStudent/{id}")
    public R delStudent(
        @PathVariable Integer id
    ){
        restTemplate.delete(id.toString(), IndexCoordinates.of("es_student"));
        return R.OK();
    }

    /**
     * 删除索引
     * @return
     */
    @PostMapping("/delIndex")
    public R delIndex(){
        IndexOperations indexOperations = restTemplate.indexOps(Student.class);
        indexOperations.delete();
        return R.OK();
    }

    /**
     * 根据文档id查询
     * @param id
     * @return
     */
    @PostMapping("/getStudentById/{id}")
    public R getStudentByID(
        @PathVariable Integer id
    ){
        IdsQueryBuilder idsQueryBuilder = QueryBuilders.idsQuery().addIds(id.toString());
        NativeSearchQuery build = new NativeSearchQueryBuilder().withQuery(idsQueryBuilder).build();
        SearchHits<Student> search = restTemplate.search(build, Student.class);
        List<SearchHit<Student>> searchHits = search.getSearchHits();
        return R.OK(searchHits);
    }

    /**
     * 学生索引批量删除
     * @param list
     * @return
     */
    @PostMapping("/delStudentAll")
    public R delStudentAll(
        @RequestBody List<Student> list
    ){
        for (Student student : list) {
            restTemplate.delete(student.getStuId(),IndexCoordinates.of("es_student"));
        }
        return R.OK();
    }

    /**
     * 生产者
     * @param msg
     * @return
     */
    @PostMapping("/sendMsg/{msg}")
    public R sendMsg(
            @PathVariable("msg") String msg
    ){
        return orderService.sendMsg(msg);
    }
}